<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<template>
  <div class="login">
    <div class="left">
      <div class="login-title">
        <img class="t1" src="@/assets/img/login-logo.png" alt />
        <span class="t2">黑马面面</span>
        <div class="t3"></div>
        <span class="t4">用户登录</span>
      </div>
      <el-form :model="form" class="form" :rules="rules" ref="form">
        <!-- 账号 -->
        <el-form-item prop="phone">
          <el-input placeholder="请输入账号" prefix-icon="el-icon-user" v-model="form.phone"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="form.password"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input placeholder="请输入验证码" prefix-icon="el-icon-key" v-model="form.code"></el-input>
            </el-col>
            <el-col :span="8">
              <img class="login-code" :src="baseUrl+codeUrl" @click="refreshCode" alt />
            </el-col>
          </el-row>
        </el-form-item>
        <!-- 用户协议 -->
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass">
            我已阅读并同意
            <el-link class="link" type="primary">用户协议</el-link>和
            <el-link class="link" type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <!-- 登录注册 -->
        <el-form-item>
          <el-button type="primary" class="btn" @click="toLogin">登录</el-button>
          <br />
          <el-button type="primary" class="btn2" @click="toregister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <img class="right" src="@/assets/img/login-right.png" alt />
    <register ref="register"></register>
  </div>
</template>

<script>
import register from "./register";
import { userLogin } from "@/api/login.js";
import { saveLocal, getLocal } from "@/utils/local.js";
export default {
  components: {
    register,
  },
  data() {
    return {
      baseUrl: process.env.VUE_APP_URL,
      codeUrl: "/captcha?type=login",
      form: {
        phone: "",
        password: "",
        code: "",
        isPass: [],
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          { min: 11, max: 11, message: "请输入11位手机号", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (reg.test(value)) {
                callback();
              } else {
                callback(new Error("请正确输入手机号"));
              }
            },
            trigger: "change",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          { min: 4, max: 12, message: "请输入6-12位密码", trigger: "change" },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "change" },
          { min: 4, max: 4, message: "请输入4位验证码", trigger: "change" },
        ],
        isPass: [{ required: true, message: "请打勾咯", trigger: "change" }],
      },
    };
  },
  created() {
    //alert(process.env.VUE_APP_URL);
    //如果有token就跳转登录页
    if (getLocal("token")) {
      this.$router.push("/layout");
    }
  },
  methods: {
    toLogin() {
      //全局表单验证
      this.$refs.form.validate((result) => {
        if (result) {
          userLogin(this.form).then((res) => {
            this.$message.success("验证通过√");
            // window.localStorage.setItem("token", res.data.data.token);
            saveLocal("token", res.data.data.token);
            this.$router.push("/layout");
            console.log(res);
          });
        } else {
          this.$message.error("验证失败:(");
        }
      });
    },
    toregister() {
      this.$refs.register.isShow = true;
    },
    //刷新验证码
    refreshCode() {
      this.codeUrl = "/captcha?type=login&dskksz=" + Date.now();
    },
  },
};
</script>

<style lang="less">
.login {
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  align-items: center;
  justify-content: space-around;
  .left {
    width: 478px;
    height: 550px;
    padding: 42px;
    background: #f5f5f5;
    .login-title {
      display: flex;
      align-items: center;
      .t1 {
        width: 25px;
        height: 18px;
      }
      .t2 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin: 0 14px;
      }
      .t3 {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
        margin-right: 12px;
      }
      .t4 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    .form {
      margin-top: 35px;
      .login-code {
        width: 100%;
        height: 40px;
      }
      .link {
        vertical-align: bottom;
      }
      .btn,
      .btn2 {
        width: 100%;
      }
      .btn2 {
        margin-top: 25px;
      }
    }
  }
}
</style>